package com.tct.worktimeboard.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tct.worktimeboard.entity.Project;
import com.tct.worktimeboard.service.IProjectService;
import com.tct.worktimeboard.util.JsonUtil;
import com.tct.worktimeboard.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyongkui
 * @since 2021-02-22
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private IProjectService projectService;

    @GetMapping("/listPage")
    public JSONObject listPagePro(Project project){
        return projectService.listPagePro(project);
    }

    @PostMapping("/set")
    public JSONObject set(@RequestBody Project project){
        int count = projectService.count(new QueryWrapper<Project>().eq("project_name", project.getProjectName()));
        if(count > 0){
            return JsonUtil.toErrorData("项目已存在");
        }
        project.setCreateTime(TimeUtil.getDateTime());
        projectService.save(project);
        return JsonUtil.toJsonData();
    }

    @PostMapping("/edit")
    public JSONObject edit(@RequestBody Project project){
        projectService.updateById(project);
        return JsonUtil.toJsonData();
    }

    @PostMapping("/del")
    public JSONObject del(@RequestBody List<Long> ids){
        projectService.removeByIds(ids);
        return JsonUtil.toJsonData();
    }
}
